package com.twu.biblioteca;

import com.twu.biblioteca.models.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    private List<Book> books;
    private List<User> users;
    private User current_user;

    public BibliotecaApp(){
        books = new ArrayList<Book>();
        users = new ArrayList<User>();
    }

    public List<Book> getAllBooks(){
        return this.books;
    }

    public void populateBooks(Book book) {
        this.books.add(book);
    }

    public String listAllBooks(){
        StringBuilder sb = new StringBuilder();
        for(Book book : books){
            sb.append(book.toString() + "\n");
        }
        return sb.toString();
    }

    public boolean reserveBookByISBN(String ISBN){
        Book foundBook = Book.findBookByISBN(books, ISBN);
        if(foundBook != null && !foundBook.isReserved()){
            foundBook.reserveBook();
            return true;
        } else
            return false;
    }

    public void addUser(User usr){
        users.add(usr);
    }

    public void setCurrentUser(User usr){
        this.current_user = usr;
    }

    public User getCurrentUser(){
        return  this.current_user;
    }

    public List<User> getUsers(){
        return this.users;
    }



    public static void main(String[] args) {

        int options;
        BibliotecaApp app = new BibliotecaApp();
        Scanner scanner = new Scanner(System.in);

        Book hgtg = new Book("A3452", "Hitchiker's guide to galaxy", "Douglas Adam");
        Book cbl = new Book("A3456", "Carbon Based Lifeform", "Douglas Adam");
        User usr1 = new User("stephen_fry","42");
        app.populateBooks(hgtg);
        app.populateBooks(cbl);
        app.addUser(usr1);

        while(true) {
            System.out.println("Welcome - Biblioteca ");
            System.out.println("1. Login");
            System.out.println("2. List all books");
            System.out.println("3. Reserve a book");
            System.out.println("4. Check Membership Details");
            System.out.println("5. Exit");
            System.out.println("Enter your choice:");
            options = scanner.nextInt();

            switch(options){
                case 1:
                    System.out.println("Enter user name:");
                    String usrname = scanner.next();
                    System.out.println("Enter password:");
                    String password = scanner.next();
                    app.setCurrentUser(User.authenticateUser(app.getUsers(), usrname, password));
                    if(app.getCurrentUser() != null)
                        System.out.println("Sucessfully Logged In");
                    else
                        System.out.println("Wrong Credentials");
                    break;
                case 2:
                    System.out.println(app.listAllBooks());
                    break;
                case 3:
                    if(app.getCurrentUser() != null){
                        System.out.println(app.listAllBooks());
                        System.out.println("Enter ISBN number of the Book:");
                        String ISBN = scanner.next();
                        if(app.reserveBookByISBN(ISBN))
                            System.out.println("Thank You! Enjoy your Book.");
                        else
                            System.out.println("Sorry we don't have that book yet.");
                        break;
                    }
                case 4:
                    System.out.println("Please talk to a Librarian. Thank you.");
                    break;
                case 5:
                    System.exit(0);
                default:
                    System.out.println("Select a valid option!!");

            }

        }
    }
}
